package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @Description TODO
 * @Author bo.li
 * @Date 2024/10/10 10:52
 * @Version 1.0
 */
@RestController
@Api(tags = "文件上传")
@RequiredArgsConstructor
public class CommonController {

    private final AliOssUtil aliOssUtil;

    //Bean的注入方式 1.属性注入  2.构造器注入  3.setter方法注入

    @PostMapping("/admin/common/upload")
    public Result upload(MultipartFile file) throws IOException {
        //处理文件名称  ddd.abcd.jpg
        String filename = file.getOriginalFilename();
        //xxxx.jpg
        //获取文件后缀
        String postfix = filename.substring(filename.lastIndexOf("."));
        String newFileName = UUID.randomUUID().toString() + postfix;

        String url = aliOssUtil.upload(file.getBytes(),newFileName );
        return Result.success(url);
    }
}